package com.huadao.cart_ccs.experiment.changeLiquidDialog;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.huadao.cart_ccs.R;
import com.huadao.cart_ccs.base.BaseDialogFragment;
import com.huadao.cart_ccs.base.BasePresenter;
import com.huadao.cart_ccs.common.TimeUtil;
import com.huadao.cart_ccs.data.bo.ChangeLiquidAmount;
import com.huadao.cart_ccs.data.task.ChangeLiquidBO;
import com.huadao.cart_ccs.data.task.ChangeLiquidTask;
import com.huadao.cart_ccs.main.CartApplication;

import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

//徐天雄的版本
public class ChangeLiquidTXDF extends BaseDialogFragment {
    @BindView(R.id.tv_operate_title)
    TextView tvOperateTitle;
    @BindView(R.id.et_firstTime)
    EditText etFirstTime;
    @BindView(R.id.et_secondTime)
    EditText etSecondTime;
    @BindView(R.id.et_thirdTime)
    EditText etThirdTime;
    @BindView(R.id.tv_operate_date)
    TextView tvOperateDate;
    @BindView(R.id.tv_operate_time)
    TextView tvOperateTime;
    @BindView(R.id.tv_people)
    TextView tvPeople;
    @BindView(R.id.tv_history_record)
    TextView tvHistoryRecord;
    @BindView(R.id.btn_operate)
    Button btnOperate;
    @BindView(R.id.btn_terminate)
    Button btnTerminate;

    private onChangeLiquid onChangeLiquid;
    private int currentTurn = 1;
    private Date operateData;
    private String operator;
    private ChangeLiquidBO changeLiquidBO;
    private ChangeLiquidTask changeLiquidTask;

    public interface onChangeLiquid {
        void onChangeLiquidFirstTurn(int firstTimeAmount, int secondTimeAmount, int thirdTimeAmount, Date data, String operator);

        void onChangeLiquidSecondTurn(int firstTimeAmount, int secondTimeAmount, int thirdTimeAmount, Date data, String operator);

        void onChangeLiquidThirdTurn(int firstTimeAmount, int secondTimeAmount, int thirdTimeAmount, Date data, String operator);

        void onTerminate();
    }

    public static ChangeLiquidTXDF newInstance(ChangeLiquidTask changeLiquidTask,int turn) {
        ChangeLiquidTXDF changeLiquidTXDF = new ChangeLiquidTXDF();
        Bundle bundle = new Bundle();
        bundle.putSerializable("changeLiquidTask", changeLiquidTask);
        bundle.putInt("turn",turn);
        changeLiquidTXDF.setArguments(bundle);
        return changeLiquidTXDF;
    }

    @Override
    protected BasePresenter getPresenter() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialogfrag_changeliquid_tx;
    }

    @Override
    protected void onInit() {
        Bundle bundle = getArguments();
        if (bundle != null) {
//            changeLiquidBO = (ChangeLiquidBO) bundle.get("ChangeLiquid");
            currentTurn = bundle.getInt("turn");
            changeLiquidTask= (ChangeLiquidTask) bundle.get("changeLiquidTask");
        }
        if(changeLiquidTask!=null) {
            changeLiquidBO = changeLiquidTask.getChangeLiquidBO();
            operateData = changeLiquidTask.getOperateDate();
            operator=changeLiquidTask.getOperator();
        }else {
            operateData=new Date();
            operator=CartApplication.getInstance().getOperator();
        }

        tvOperateDate.setText(TimeUtil.date_sdf.format(operateData));
        tvOperateTime.setText(TimeUtil.time_sdf.format(operateData));
        tvPeople.setText(operator);

        if (changeLiquidBO != null) {
            btnTerminate.setVisibility(View.VISIBLE);
            etFirstTime.setEnabled(false);
            etSecondTime.setEnabled(false);
            etThirdTime.setEnabled(false);
            List<ChangeLiquidBO.ChangeLiquidTurn> changeLiquidTurnList = changeLiquidBO.getChangeLiquidTurnList();
            if (changeLiquidTurnList == null || changeLiquidTurnList.size() == 0) {
                return;
            }
            if (changeLiquidTurnList.size() > 0) {
                ChangeLiquidBO.ChangeLiquidTurn changeLiquidTurn = changeLiquidTurnList.get(0);
                List<ChangeLiquidAmount> firstTurnList = changeLiquidTurn.getAmountList();
                if (firstTurnList.size() > 0) {
                    etFirstTime.setText(String.valueOf(firstTurnList.get(0).getFluid()));
                } else {
                    etFirstTime.setText("0");
                }
                if (firstTurnList.size() > 1) {
                    etSecondTime.setText(String.valueOf(firstTurnList.get(1).getFluid()));
                } else {
                    etSecondTime.setText("0");
                }
                if (firstTurnList.size() > 2) {
                    etThirdTime.setText(String.valueOf(firstTurnList.get(2).getFluid()));
                } else {
                    etThirdTime.setText("0");
                }
            }
            btnOperate.setText("第" + currentTurn + "轮置换中...");
            btnOperate.setBackgroundResource(R.drawable.btn_info_bg);
            btnOperate.setEnabled(false);
        }else {
            btnOperate.setText("开始第" + currentTurn + "轮置换");
            tvOperateTitle.setText("置换" + "(第" + currentTurn + "轮)");
            btnTerminate.setVisibility(View.GONE);
        }

        getDialog().setCanceledOnTouchOutside(false);
    }

    @Override
    protected boolean isDialog() {
        return true;
    }

    @OnClick(R.id.btn_operate)
    public void onViewClicked() {
        String err = checkParams();
        if (!TextUtils.isEmpty(err)) {
            showDialogMessage(null, err);
            return;
        }
        if (onChangeLiquid == null) {
            return;
        }
        int firstTime = Integer.parseInt(etFirstTime.getText().toString());
        int secondTime = Integer.parseInt(etSecondTime.getText().toString());
        int thirdTime = Integer.parseInt(etThirdTime.getText().toString());
        if (currentTurn == 1) {
            onChangeLiquid.onChangeLiquidFirstTurn(firstTime, secondTime, thirdTime, operateData, CartApplication.getInstance().getOperator());
        } else if (currentTurn == 2) {
            onChangeLiquid.onChangeLiquidSecondTurn(firstTime, secondTime, thirdTime, operateData, CartApplication.getInstance().getOperator());
        } else if (currentTurn == 3) {
            onChangeLiquid.onChangeLiquidThirdTurn(firstTime, secondTime, thirdTime, operateData, CartApplication.getInstance().getOperator());
        }
//        dismiss();

        btnOperate.setText("第" + currentTurn + "轮置换中...");
        btnOperate.setBackgroundResource(R.drawable.btn_info_bg);
        btnOperate.setEnabled(false);
        btnTerminate.setVisibility(View.VISIBLE);

        etFirstTime.setEnabled(false);
        etSecondTime.setEnabled(false);
        etThirdTime.setEnabled(false);
    }

    @OnClick(R.id.btn_close)
    public void closeClock() {
        dismiss();
    }
    private String checkParams() {
        String err = null;
        if (TextUtils.isEmpty(etFirstTime.getText().toString())) {
            err = "请输入第一次置换体积";
        } else if (TextUtils.isEmpty(etSecondTime.getText().toString())) {
            err = "请输入第二次置换体积";
        } else if (TextUtils.isEmpty(etThirdTime.getText().toString())) {
            err = "请输入第三次置换体积";
        }
        return err;
    }

    @OnClick(R.id.btn_terminate)
    public void terminate(){
        if(onChangeLiquid!=null){
            onChangeLiquid.onTerminate();
        }
        this.dismiss();
    }

    public ChangeLiquidTXDF.onChangeLiquid getOnChangeLiquid() {
        return onChangeLiquid;
    }

    public void setOnChangeLiquid(ChangeLiquidTXDF.onChangeLiquid onChangeLiquid) {
        this.onChangeLiquid = onChangeLiquid;
    }

    //完成一轮置换
    public void finishOneTurn() {
        currentTurn++;
        initText();
    }

    private void initText() {
        tvOperateTitle.setText("置换" + "(第" + currentTurn + "轮)");
        btnOperate.setText("开始第" + currentTurn + "轮置换");
    }
}
